#include"LinkStack.h"
#include<stdio.h>
#include<stdlib.h>
#define true 1
#define false 0

int IsOper(char c)
{
    switch(c)
    {
        case '+':
        case '-':
        case '*':
        case '/':
        case '=':
        case '(':
        case ')':
            return true;
        default:
            return false;
    }
}

int IsNum(char c)
{
    if(c>='0'&&c<='9')
        return true;
    else
        return false;
}

int Operate(int prev,int next,char symbol)
{
    switch(symbol)
    {
        case '+':return prev+next;
        case '-':return prev-next;
        case '*':return prev*next;
        case '/':
            if(next==0)
            {
                printf("devide zero!\n");
                exit(-1);
            }
            return prev/next;
        default:
            return false;
    }   
}

char Precede(char ch1,char ch2)
{
    int i=0,j=0;
    switch(ch1)
    {
        case '+':i=0;break;
        case '-':i=1;break;
        case '*':i=2;break;
        case '/':i=3;break;
        case '(':i=4;break;
        case ')':i=5;break;
        case '=':i=6;break;
        default:
            break;       
    }

    switch(ch2)
    {
        case '+':j=0;break;
        case '-':j=1;break;
        case '*':j=2;break;
        case '/':j=3;break;
        case '(':j=4;break;
        case ')':j=5;break;
        case '=':j=6;break;
        default:
            break;       
    }

    char pre[7][7]=
    {
        {'>','>','<','<','<','>','>'},
        {'>','>','<','<','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'<','<','<','<','<','=','0'},
        {'>','>','>','>','0','>','>'},
        {'<','<','<','<','<','0','='}
    };
    return pre[i][j];
}

int main()
{
    LStack Num;
    InitLinkStack(&Num);
    LStack Oper;
    InitLinkStack(&Oper);
    // 给栈中压入一个垃圾值避免出现栈里没东西就取出现段错误
    Push(&Oper,'=');

    char str[100]={0};
    scanf("%s",str);

    int i=0;
    while(str[i]!='\0')
    {
       if(IsOper(str[i]))
       {
            switch(Precede(GetTop(&Oper)->data,str[i]))
            {
                case '<':
                    Push(&Oper,str[i]);
                    i++;
                    break;
                case '>':
                    int next=*Pop(&Num);
                    int prev=*Pop(&Num);
                    Push(&Num,Operate(prev,next,*Pop(&Oper)));
                    break;
                case '=':
                    Pop(&Oper);
                    i++;
                    break;
                default:
                    break;
            }
       }
       else if(IsNum(str[i]))
       {
            int lower=str[i]-'0';
            Push(&Num,lower);
            i++;
            while(IsNum(str[i]))
            {
                int num=str[i]-'0';
                int higher=*Pop(&Num);
                Push(&Num,higher*10+num);
                i++;
            }
       }
       else if(str[i]==' ')
       {
            while(str[i]==' ')
            {
                i++;
            }
       }
    }
    printf("the answer is %d\n",*Pop(&Num));
    return 0;
}